(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:MainCtrl
     * @description
     * # MainCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('MainCtrl', MainCtrl);

    MainCtrl.$inject = ['$scope', 'dataService', 'helper', 'dhfprinterService', '$timeout', '$state', '$rootScope', 'sessionService'];

    function MainCtrl($scope, dataService, helper, dhfprinterService, $timeout, $state, $rootScope, sessionService) {
        $scope.indexAction = function() {
            $scope.LoadHOme();
            
            $scope.ary = {};
            //管理员不显示
            $rootScope.$on('event:up-store-id', function() {
                $scope.LoadHOme();
            });
        }

        //加载首页数据
        $scope.LoadHOme = function() {
            var user = sessionService.get("tokenUser");
//             console.error("useruser", user);
            if (user && user.Role == 4) {

            } else {

                $state.go("home.index");
            }
        }

        function testdata() {

            var height = 480;
            var width = 869;
            var pheight = Math.round(height / 300);
            var pwidth = Math.round(width / 300);
            var request_id = helper.guid();

            return {
                // review: true, //是否预览，true时，同步返回pdf文件的base64结果
                sync: false, //是否同步打印，true：等待打印机打印完毕返回；false：任务提交给打印机返回，不能确定是否真实打印成功。默认false
                request_id: request_id, //每次请求的唯一id，如果是相同的打印内容，可以使用同一个request_id防止重复
                html: '<html><title>' + request_id + '</title><style>@page {margin: 0px;}</style><body style="margin:0px;border: solid 1px #000;"><div style="background-color: #ccc;width:' + width + 'px;height:' + height + 'px;">测试打印页面内容' + random(99999999) + '</div></body></html>',
                height: height, //打印的页面高度，单位px
                width: width, //打印的页面宽度，单位px
                // printer: "EPSON LQ-730K ESC/P 2", //打印机名称，通过dhfprinterService.getPrinters获取
                printer: "HP_LaserJet_Professional_P1108", //打印机名称，通过dhfprinterService.getPrinters获取
                parms: {
                    name: "大黄蜂", //其他参数，暂不用
                    title: "中国", //其他参数，暂不用
                },
            };
        }

        //测试打印
        $scope.print = function() {
            var testData = testdata();
            testData.review = false; //不预览，直接打印
            dhfprinterService.print(testData).then(function(a) {
                console.log("返回结果：", a);
            });
        }

        //测试预览
        $scope.printreview = function() {
            var testData = testdata();
            testData.review = true; //预览，不打印
            dhfprinterService.print(testData).then(function(a) {
                console.log("返回结果：", a);
            });
        }

        //打印机
        $scope.printers = function() {
            dhfprinterService.getPrinters().then(function(a) {
                console.log("返回结果：", a);
            });
        }

        $scope.index1Action = function() {

            $scope.staticData = {
                trade: {
                    all: 0,
                    status: []
                },
                out: {
                    all: 0,
                    status: []
                },
                in : {
                    all: 0,
                    status: []
                },
                stock: {
                    all: 0,
                    status: []
                },
            };


            //订单总数
            dataService.tradeGet({
                page_size: 1
            }).then(function(trades) {
                $scope.staticData.trade.all = trades.total_count;
            });

            // //订单各个状态数量
            // dataService.statusGet("trade").then(function(status) {
            //     _.map(status, function(a) {
            //         if (a.hide != 0) return;
            //         dataService.tradeGet({
            //             query: {"status" : a.status},
            //             page_size: 1
            //         }).then(function(b) {
            //             $scope.staticData.trade.status.push({
            //                 state: a,
            //                 count: b.total_count,
            //             });
            //             console.log("------", $scope.staticData);
            //         });
            //     });
            // });

            //订单总数
            dataService.stockoutGet({
                page_size: 1
            }).then(function(outs) {
                $scope.staticData.out.all = outs.total_count;
            });
            //订单总数
            dataService.stockinGet({
                page_size: 1
            }).then(function(ins) {
                $scope.staticData.in.all = ins.total_count;
            });
            //订单总数
            dataService.stockGet({
                page_size: 1
            }).then(function(stocks) {
                $scope.staticData.stock.all = stocks.total_count;
            });

        }

    }
})();